package com.smrs.goods.dao;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Query;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.smrs.basicdata.model.Department;
import com.smrs.goods.model.GoodsCategoryModel;

public class GoodsCategoryDao extends BaseDAOHibernateImpl<GoodsCategoryModel, Long>{
	public List<GoodsCategoryModel> getSubCategoryByParent(Long parentId,Long level){
		String hsql ="";
		ArrayList values =new ArrayList();
		if(parentId==null || parentId.equals(0l)){
			hsql = " from GoodsCategoryModel where  level=1";
			//values = new Object[]{};
		}
		if(parentId!=null){
			hsql = " from GoodsCategoryModel where 1=1 and  parent.id=? ";
			 values.add(parentId);
		}
		if(level!=null && !Long.valueOf(0l).equals(level)){
			hsql = hsql + " and level = ? ";
			 values.add(level);
		}
		Query query = this.createHQLQuery(hsql, values.toArray());
		return query.list();
	}
	

	/**
	 * 得到父节点的链条
	 * @param modelId
	 * @return
	 */
	public LinkedList<GoodsCategoryModel> getParentsChain(Long modelId) {
		LinkedList<GoodsCategoryModel> departments = new LinkedList<GoodsCategoryModel>();
		GoodsCategoryModel resource = get(modelId);
		if(resource == null){
			return departments;
		}
		GoodsCategoryModel tmpDepartment = resource;
		while(tmpDepartment.getParent() != null && !tmpDepartment.getParent().getId().equals(tmpDepartment.getId())){
			departments.addFirst(tmpDepartment.getParent());
			tmpDepartment = tmpDepartment.getParent();
			initProxyObject(tmpDepartment);
		}
		departments.addLast(resource);
		return departments;
	}
	
	
	
}
